App({

  globalData: {
    baseUrl: 'https://we9.91gongju.cn:10000',
    //baseUrl: 'https://localhost:10000',
    a: {}
  },

  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function (z) {
    let a = z.query;
    this.globalData.a = a;
  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function (options) {
    console.log(options);
  },
  globalHandleLogin: function (e) {
    console.log('获取信息', e)
    var that = this;
    that.login();
    that.globalData.userInfoData = e.detail.userInfo
    wx.setStorageSync('userInfo', e.detail.userInfo)
    wx.setStorageSync('openid', e.detail.userInfo.openid);
    wx.setStorageSync('nick_name', e.detail.userInfo.nickName);
    wx.setStorageSync('avatar_url', e.detail.userInfo.avatarUrl);
    wx.setStorageSync('gender', e.detail.userInfo.gender);
    //if (!e.detail.userInfo) {

    //   that.globalData.userInfoData =e.detail.userInfo
    //   wx.setStorageSync('userInfo', e.detail.userInfo)
    //   wx.setStorageSync('openid', e.detail.userInfo.openid);
    //   wx.setStorageSync('nick_name', e.detail.userInfo.nickName);
    //   wx.setStorageSync('avatar_url', e.detail.userInfo.avatarUrl);
    //   wx.setStorageSync('gender', e.detail.userInfo.gender);
    // } else if (wx.getStorageSync('userInfo')) {
    //   that.globalData.userInfoData = wx.getStorageSync('userInfo')
    // }
  },
  login: function () {
    var that = this;
    wx.login({
      success(res) {
        console.log('login调用成功', res.code);
        var code = res.code;
        wx.request({
          url: `https://we9.91gongju.cn:10000/oauth/wx/getOpenid/${code}`,
          method: 'GET',
          dataType: 'json',
          header: {
            'content-type': 'application/json'
          },
          success: (res) => {
            console.log('成功获取openid', JSON.parse(res.data.message).openid)
            wx.setStorageSync('openid', JSON.parse(res.data.message).openid);
            wx.request({
              url: `${that.globalData.baseUrl}/user/selectIsUser/${wx.getStorageSync('openid')}`, // 目标服务器url
              method: 'GET',
              dataType: 'json',
              header: {
                'content-type': 'application/json'
              },
              success: (res) => {
                console.log('数据库中是否存在用户数据', res.data);
                if (res.data.message === '用户不存在') {
                  that.register();
                } else {
                  that.globalData.userInfoData = wx.getStorageSync('userInfo')

                  // wx.request({
                  //   url: `${app.globalData.baseUrl}/message/count/${res.data.dataMap.data.id}`,
                  //   success(res1) {
                  //     console.log("message: " + res1.data.dataMap.data);
                  //     that.setData({message: res1.data.dataMap.data})
                  //   }
                  // })
                }
              }
            });
          },
          fail(res) {
            console.log('获取openid失败');
          }
        })
      },
      fail(res) {
        console.log(`login调用失败`);
      },
    });
  },
  register: function () {
    wx.request({
      url: `https://we9.91gongju.cn:10000/user/register`, // 目标服务器url
      method: 'POST',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: wx.getStorageSync('openid'),
        nickName: wx.getStorageSync('nick_name'),
        avatarUrl: wx.getStorageSync('avatar_url'),
        gender: wx.getStorageSync('gender'),
      },
      success: (res) => {
        var constant = res.data.message
        this.invatation();
        this.addIntegral();
      }
    });
  },

  addIntegral: function () {
    wx.request({
      url: `https://we9.91gongju.cn:10000/user/getInfo/${wx.getStorageSync('openid')}`,
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: (result) => {
        wx.request({
          url: `https://we9.91gongju.cn:10000/integral/addIntegral/${result.data.dataMap.data.id}`,
          method: 'POST',
          dataType: 'json',
          success: (result) => {
            console.log('添加积分调用成功', result);
          },
          fail: () => {
            console.log('添加积分调用失败');
          }
        });
      },
      fail: () => { }
    });
  },
  invatation() {
    let a = this.globalData.a;
    if (a.refUser) {
      wx.request({
        url: `https://we9.91gongju.cn:10000/integral`,
        complete: (b) => { },
        data: {
          "category": "1",
          "origin": "邀请用户",
          "user": a.r,
          "value": 2
        },
        method: "POST",
        success: (b) => {
          wx.request({
            url: `https://we9.91gongju.cn:10000/message/save`,
            complete: (c) => { },
            data: {
              "message": "邀请用户",
              "user": a.r,
            },
            fail: (c) => { },
            method: "POST",
            success: (c) => {
              wx.hideLoading({
                complete: (d) => {
                  wx.showToast({
                    title: '被邀请成功',
                  })
                },
              })
            },
          })
        },
      })
    }
  }
})